Page({
  onLoad({
    id
  }) {
    console.log(id, 11111);
    this.getHouseList()
    this.getRepairItem()
    if (id) {
      wx.setNavigationBarTitle({
        title: '修改报修信息',
      })
      this.getRepairDetail(id)
    }
  },
  data: {
    appointment: '', //选择日期
    houseInfo: '', //房屋信息
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    repairItemName: '',
    repairItemId: '',
    mobile: '',
    description: '' //报修内容
  },
  openHouseLayer() {
    this.setData({
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },
  openRepairLayer() {
    this.setData({
      repairLayerVisible: true
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  selectHouse(e) {
    // console.log(e.detail.name);
    const {
      name,
      id: houseId
    } = e.detail
    this.setData({
      houseInfo: name,
      houseId
    })
  },
  //选择报修项目
  selectHouseItem(e) {
    // console.log(e.detail);
    const {
      id: repairItemId,
      name: repairItemName
    } = e.detail
    this.setData({
      repairItemName,
      repairItemId
    })
  },
  //选择日期
  selectDate(e) {
    // console.log(e);
    const time = e.detail
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.formatDate(time)
    })
  },
  //问题附件，图片
  afterRead(event) {
    const {
      file
    } = event.detail;
    wx.uploadFile({
      url: 'https://live-api.itheima.net/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: `Bearer ${getApp().token}`
      },
      success: (res) => {
        // 上传完成需要更新 attachment
        const {
          data
        } = JSON.parse(res.data)
        const {
          attachment = []
        } = this.data
        attachment.push({
          ...data
        });
        this.setData({
          attachment
        });
      },
    });
  },
  //获取审核ton过的房屋
  async getHouseList() {
    const {
      data
    } = await wx.http.get('/house')
    console.log(data, 1111);
    this.setData({
      houseList: data
    })
  },
  // 获取报修项目
  async getRepairItem() {
    const {
      data
    } = await wx.http.get('/repairItem')
    this.setData({
      repairItem: data
    })
  },
  // 选择报修房屋
  checkHouse() {
    const valid = this.data.houseInfo !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择房屋信息!')
    // 返回验证结果
    return valid
  },
  // 选择报修项目
  checkRepairItem() {
    const valid = this.data.repairItemId !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择报修项目!')
    // 返回验证结果
    return valid
  },
  //填写联系手机号
  checkMobile() {
    const reg = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
    const valid = reg.test(this.data.mobile.trim())
    if (!valid) wx.utils.toast('请填写手机号！')
    // 返回验证结果
    return valid
  },
  //验证预约报修日期
  checkDate() {
    const reg = /^\d{4}[-|/]\d{2}[-|/]\d{2}$/
    const valid = reg.test(this.data.appointment)
    if (!valid) wx.utils.toast('请选择维修日期')
    // 返回验证结果
    return valid
  },
  // 选择报修内容
  checkDesc() {
    const valid = this.data.description.trim() !== ''
    if (!valid) wx.utils.toast('请输入问题描述')
    // 返回验证结果
    return valid
  },
  checkPicture() {
    const valid = this.data.attachment.length > 0
    if (!valid) wx.utils.toast('请上传报修附件')
    // 返回验证结果
    return valid
  },
  async onSubmit() {
    if (!this.checkHouse()) return
    if (!this.checkRepairItem()) return
    if (!this.checkMobile()) return
    if (!this.checkDate()) return
    if (!this.checkDesc()) return
    if (!this.checkPicture()) return
    //有id的情况下是编辑报修信息，没有则是添加
    const {
      id,
      houseId,
      repairItemId,
      appointment,
      mobile,
      description,
      attachment
    } = this.data
    await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      appointment,
      mobile,
      description,
      attachment
    })
    wx.utils.toast('提交成功')
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
  async getRepairDetail(id) {
    const res = await wx.http.get(`/repair/${id}`)
    console.log(res);
    this.setData({
      ...res.data
    })
  }
})